﻿using GatewayClient.MQ;
using Iot.Device.Sht3x;
using IotDeviceToolHepler;
using IotDeviceToolHepler.Device;
using IotDeviceToolHepler.Model.Device;
using Models.Common;
using Models.Constant;
using Models.Device;
using Models.TaskPlain;
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Data;
using System.Device.I2c;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading;
using UtilsBase;
using static Models.Common.EnumDeviceState;

namespace GatewayClient.Device
{
    public class ServiceDeviceSensorSHT301004
    {


        public void gateWayExecuteCommand(object obj)
        {
            MQReqModel<object> mQReqModel = (MQReqModel<object>)obj;
            CommandLogModel commandLogModel = mQReqModel.toGatewayData;
            ModelSht30Info modelSht30Info = new ModelSht30Info();
            ModelReceverData modelReceverData = new ModelReceverData();
            ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
            ModelCommandAgreementConent modelCommandAgreementConent = UtilsObject.DynamicToEntity<ModelCommandAgreementConent>(modelCommandAgreement.content);

            try
            {
                List<ModelCommandResult> results = new List<ModelCommandResult>();
                ModelCommandResult result = new ModelCommandResult();


                if (commandLogModel.agreementType == EnumAgreementType.I2C)
                {
                    ModelCommandAgreementParameter modelI2C = modelCommandAgreement.agreementParameter;
                    modelSht30Info = Sensor_SHT30.getTempAndHum(modelI2C.busId);
                    // modelReceverData = new ModelReceverData();
                    //  modelReceverData.humidity = modelSht30Info.humidity;
                    // modelReceverData.temperature = modelSht30Info.temperature;

                    result = new ModelCommandResult();
                    result.name = "湿度";
                    result.key = "humidity";
                    result.value = modelSht30Info.humidity.ToString("0.0");
                    results.Add(result);

                    result = new ModelCommandResult();
                    result.name = "温度";
                    result.key = "temperature";
                    result.value = modelSht30Info.temperature.ToString("0.0");
                    results.Add(result);

                    modelReceverData.commandResults = results;
                    mQReqModel.respCode = ResponseCode.SUCCESS;
                    mQReqModel.respMsg = MQEventEnum.Event_State_OK;
                    mQReqModel.toServerData = modelReceverData;
                    new ServiceMQ_Helper().sendMsg(mQReqModel);

                    // Console.WriteLine("采集sht30" + JsonConvert.SerializeObject(mQReqModel));
                }
            }
            catch (Exception ex)
            {

                //new GatewayFileHelper().log_WriteMessageAppend("error", "" + ex.ToString());
                Console.WriteLine(ex.ToString());
            }

        }





    }

}
